package com.kzd.gtboon.main.popup

import android.content.Context
import android.view.View
import android.widget.ProgressBar
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.kzd.gtboon.R
import com.kzd.gtboon.custom.setOnClickAntiShakeListener
import razerdp.basepopup.BasePopupWindow
import rxhttp.wrapper.entity.Progress
import java.text.DecimalFormat

class ProgressPopup(
    context: Context,
    val onConfirm: () -> Unit,
    val onCancel: () -> Unit
) : BasePopupWindow(context) {

    private var clProgressBody: ConstraintLayout? = null
    private var mProgressBar: ProgressBar? = null
    private var tvKeepPercent: TextView? = null
    private var tvKeepFileSize: TextView? = null
    private var tvProgressMessage: TextView? = null
    private var tvProgressCancel: TextView? = null
    private var tvProgressConfirm: TextView? = null

    init {
        isOutSideTouchable = false
        setBackPressEnable(false)
        setOutSideDismiss(false)
    }

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_progress)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        clProgressBody = contentView.findViewById(R.id.clProgressBody)
        mProgressBar = contentView.findViewById(R.id.mProgressBar)
        tvKeepPercent = contentView.findViewById(R.id.tvKeepPercent)
        tvKeepFileSize = contentView.findViewById(R.id.tvKeepFileSize)
        tvProgressMessage = contentView.findViewById(R.id.tvProgressMessage)
        tvProgressCancel = contentView.findViewById(R.id.tvProgressCancel)
        tvProgressConfirm = contentView.findViewById(R.id.tvProgressConfirm)

        tvProgressCancel?.setOnClickAntiShakeListener {
            onCancel()
        }
        tvProgressConfirm?.setOnClickAntiShakeListener {
            onConfirm()
        }
    }

    fun setProgress(progress: Progress): ProgressPopup {
        mProgressBar?.progress = progress.progress
        tvKeepPercent?.text = StringBuilder()
            .append(progress.progress).append("%")
        tvKeepFileSize?.text = StringBuilder()
            .append(bytes2kb(progress.currentSize))
            .append("/")
            .append(bytes2kb(progress.totalSize))
        return this
    }

    fun resetProgress() {
        tvProgressMessage?.visibility = View.GONE
        clProgressBody?.visibility = View.VISIBLE
        tvProgressConfirm?.visibility = View.GONE
        tvProgressCancel?.visibility = View.VISIBLE
        mProgressBar?.progress = 0
        tvKeepPercent?.text = "0"
        tvKeepFileSize?.text = "0"
    }

    companion object {
        private const val GB: Long = 1024 * 1024 * 1024
        private const val MB: Long = 1024 * 1024
        private const val KB: Long = 1024

        fun bytes2kb(bytes: Long): String = when {
            bytes / GB >= 1 -> "${DecimalFormat("###.0").format(bytes / GB)}GB"
            bytes / MB >= 1 -> "${DecimalFormat("###.0").format(bytes / MB)}MB"
            bytes / KB >= 1 -> "${DecimalFormat("###.0").format(bytes / KB)}KB"
            else -> "$${bytes}B"
        }
    }
}